<template>
	<view class="u-page">
		<u-list @scrolltolower="scrolltolower">
			<u-list-item v-for="(item, index) in indexList" :key="index">
				<u-cell>
					<view class="" slot="title" style="display: flex;justify-content: space-between;"
						@click="tapMsg(item,index,'user')">
						<view class="">
							{{item.nickname}}
						</view>
						<view class="" >
							<u--text :lines="1" size="0.8em" type="info" align="right" style="text-align: end;" :text="item.time">
							</u--text>
						</view>
					</view>
					<view class="" slot="icon" style="position:relative;width: 40px;"
						@click="tapMsg(item,index,'image')">
						<view class="" style="position: absolute;margin-left: 25px;margin-top: -10px;z-index: 100;">
							<u-badge max="99" :value="item.count"></u-badge>
						</view>
						<u-avatar shape="square" size="35" :src="item.avatar" customStyle="margin: -3px 5px -3px 0">
						</u-avatar>
					</view>
					<!-- <text slot="value" class="u-slot-value">99</text> -->

					<view class="" slot="label" style="font-size: 0.8em;color: #666;" @click="tapMsg(item,index,'msg')">
						<!-- {{item.message}} -->
						<u--text :lines="1" size="0.8em" type="info" :text="item.content"></u--text>
					</view>
				</u-cell>
			</u-list-item>
		</u-list>
	</view>
</template>

<script>
	import {
		// sendToChat,
		// readChat,
		// getChat,
		// list,
		// oneUser,
		unread,
		updateReadState
	} from "@/api/chat"
	import {
		dataFormat
	} from "@/utils/yc/Time.js"
	export default {
		data() {
			return {
				oldloading: false,
				hasOldMsg: true,
				msgList: []
				// indexList: [],
				// msgList: [{
				// 	uid: 72,
				// 	nickname: "北药商贸",
				// 	avatar: 'https://cdn.uviewui.com/uview/album/1.jpg',
				// 	content: "辽宁北药商贸股份有限公司于2012年07月18日在沈阳市工商行政管理局登记成立。法定代表人牛瑞，公司经营范围包括中成药、中药材、中药饮片、化学原料药、化学药制剂等。",
				// 	sendTime: "2月19日"
				// }]
			}
		},
		computed: {
			indexList() {
				var msgs = this.msgList;
				var last = null
				msgs.filter(e => {
					var time = new Date(e.sendTime).valueOf();
					var theTime = dataFormat(parseInt(time / 1000))
					if (!last || last.time && last.time != theTime) {
						e.time = theTime
					}
					last = JSON.parse(JSON.stringify(e))
					var ipos = theTime.indexOf("年"); //指定开始的字符串
					e.time = theTime.substring(ipos + 1, theTime.length); //取前部分（指定开始的字符串的之前）
					return e
					// last.time = theTime
				})
				return msgs
			}
		},
		onLoad() {
			// this.loadmore()
			this.msgList = uni.getStorageSync("msgList") || []
			this.getMessage({})
		},
		methods: {
			tapMsg(item, index, type) {
				item.uid = item.sender
				var q = {
					sender: item.sender
				}
				var msg = []
				var msgList = this.msgList.filter(res => {
					if (res.id == item.id && res.count>0) {
						res.count = 0
						msg.push(res)
					} else {
						return res
					}
				})
				
				this.updateList(msgList)
				updateReadState(q).then(e => {
					this.getMessage({})
				})
			},
			scrolltolower() {
				console.log("到底部")
				// this.loadmore()
			},
			loadmore() {
				for (let i = 0; i < 3; i++) {
					this.indexList = this.indexList.concat(this.msgList)
				}
			},
			updateList(newList) {
				var msgList = this.msgList
				// 返回数据不足 设置没有历史数据
				for (var i = 0; i < newList.length; i++) {
					msgList = msgList.filter(item => {
						if (item.sender != newList[i].sender) {
							return item
						}
					})
					msgList.push(newList[i])
				}
				// 下面代碼把已读信息放都后面，未读放到前面
				var list1 = []
				msgList = msgList.filter(item => {
					if (item.count) {
						list1.push(item)
					} else {
						return item
					}
				})
				list1=list1.concat(msgList)
				// 缓存到本地
				uni.setStorageSync("msgList", list1)
				this.msgList = list1
				return list1
			},
			getMessage(params) {
				if (this.oldloading == false && this.hasOldMsg) {
					this.oldloading = true
					unread(params).then(res => {
						if (res.data.length > 0) {
							// var msg = res.data
							this.updateList(res.data)
							this.oldloading = false
						}
					})
				}
			}
		},
	}
</script>
